package weektwo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubdomainVisits_811 {
    public List<String> subdomainVisits(String[] cpdomains) {
        Map<String, Integer> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        for (String s : cpdomains) {
            int spaceIndex = s.indexOf(' ');
            // String[] tmp = s.split(" ");
            int val = Integer.parseInt(s.substring(0, spaceIndex));
            String domain = s.substring(spaceIndex + 1);
            while (true) {
                // System.out.println(domain + ": " + val);
                if (map.containsKey(domain))
                    map.put(domain, map.get(domain) + val);
                else
                    map.put(domain, val);
                int index = domain.indexOf('.');
                // System.out.println("index: " + index);
                if (index < 0) break;
                domain = domain.substring(index + 1);
            }
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            list.add(entry.getValue() + " " + entry.getKey());
        }
        return list;

    }
}
